package com.chuji.crmcj.act;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;

import com.chuji.crmcj.R;
import com.chuji.crmcj.fragment.BaseFragment;
import com.chuji.crmcj.fragment.MagHomeFragment;
import com.chuji.crmcj.fragment.MagManFragment;
import com.chuji.crmcj.fragment.MineFragment;
import com.chuji.crmcj.fragment.SaleHomeFragment;
import com.chuji.crmcj.fragment.TaskFragment;
import com.chuji.crmcj.utils.SPUtils;
import com.chuji.crmcj.utils.UIUtils;
import com.chuji.crmcj.view.LazyViewPager;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends FragmentActivity {
    //    private ViewPager mViewPager;
    private LazyViewPager mViewPager;
    private RadioGroup main_radio;
    //默认展示首页
    private int mCurrentItem = 0;
    private List<BaseFragment> fragments;
    private RelativeLayout mRlSearchBar;
    private RadioButton mRbFollow;
    private String userID;
    private RadioButton mRbFunction;
    private int roleType;
    private RadioButton mRbSet;
    public static MainActivity instance = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

//        mViewPager = (ViewPager) findViewById(R.id.layout_content);
        mViewPager = (LazyViewPager) findViewById(R.id.layout_content);
        mRbFunction = (RadioButton) findViewById(R.id.rb_function);
        main_radio = (RadioGroup) findViewById(R.id.main_radio);
        mRbFollow = (RadioButton) findViewById(R.id.rb_follow);
        mRbSet = (RadioButton) findViewById(R.id.rb_setting);

        instance = this;

        mViewPager.setOffscreenPageLimit(2);
        initData();
        initListener();
    }

    private void initData() {
        roleType = SPUtils.getInt(UIUtils.getContext(), "RoleType", -1);
//        userID = "1227FC37-8E05-43ED-BE86-0CE5A401FD6F";
//        SPUtils.saveString(UIUtils.getContext(), "userID", userID);
//        SPUtils.saveInt(UIUtils.getContext(), "roleType", roleType);
//        roleType=1;
//        String companyID = "C04B3AE6-1866-4490-9D73-FAB0DB121F12";
//        SPUtils.saveString(UIUtils.getContext(), "CompanyID", companyID);
//        String customerID = "2D5640C9-EB6B-4A2B-9147-581B3D059A86";
//        SPUtils.saveString(UIUtils.getContext(), "CustomerID", customerID);
//        Drawable drawable1=getResources().getDrawable(R.drawable.home);
//        drawable1.setBounds(0, 0,UIUtils.dip2px(25),UIUtils.dip2px(25));//第一0是距左边距离，第二0是距上边距离，40分别是长宽
//        mRbFunction.setCompoundDrawables(null,drawable1,null,null);//只放左边
        if (roleType != 1) {
            //经理  总经理端
            mRbFollow.setText("管理");
            mRbFollow.setCompoundDrawablesWithIntrinsicBounds(0, R.drawable.mag_select, 0, 0);
//            mRbFollow.setCompoundDrawablePadding(UIUtils.dip2px(0));
            mRbFollow.setPadding(0, UIUtils.dip2px(10), 0, 0);
        }
        fragments = new ArrayList<BaseFragment>();
        if (roleType == 1) {
            fragments.add(new SaleHomeFragment());
            fragments.add(new TaskFragment());
            fragments.add(new MineFragment());
        } else if (roleType == 2 || roleType == 3) {
            fragments.add(new MagHomeFragment());
            fragments.add(new MagManFragment());
            fragments.add(new MineFragment());
        }
        HomeAdapter mAdapter = new HomeAdapter(getSupportFragmentManager());
        mViewPager.setAdapter(mAdapter);

        //初始化
//        mViewPager.setCurrentItem(mCurrentItem);
//        main_radio.check(mCurrentItem);
        main_radio.check(R.id.rb_function);
    }

    private void initListener() {
        mViewPager.setOnPageChangeListener(new LazyViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float v, int i1) {
//                switch (position) {
//                    case 0:
////                        mRbFunction.setChecked(true);
//                        main_radio.check(R.id.rb_function);
//                        break;
//                    case 1:
////                        mRbFollow.setChecked(true);
//                        main_radio.check(R.id.rb_follow);
//                        break;
//                    case 2:
////                        mRbSet.setChecked(true);
//                        main_radio.check(R.id.rb_setting);
//                        break;
//                }
            }

            @Override
            public void onPageSelected(int position) {
                mCurrentItem = position;
                switch (position) {
                    case 0:
//                        mRbFunction.setChecked(true);
                        main_radio.check(R.id.rb_function);
                        break;
                    case 1:
//                        mRbFollow.setChecked(true);
                        main_radio.check(R.id.rb_follow);
                        break;
                    case 2:
//                        mRbSet.setChecked(true);
                        main_radio.check(R.id.rb_setting);
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });

        main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_function:
                        mViewPager.setCurrentItem(0);
                        //设置滑动菜单不运行滑动
                        break;
                    case R.id.rb_follow:
                        mViewPager.setCurrentItem(1);
                        break;
                    case R.id.rb_setting:
                        mViewPager.setCurrentItem(2);
                        break;
                }
            }
        });
    }

    private class HomeAdapter extends FragmentPagerAdapter {
        public HomeAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            return fragments.get(i);
        }

        @Override
        public int getCount() {
            return 3;
        }

//        private List<BasePage> mLists;
//
//        public HomeAdapter(List<BasePage> mDatas) {
//            this.mLists = mDatas;
//        }
//
//        @Override
//        public void destroyItem(ViewGroup container, int position, Object object) {
//           container.removeView(mLists.get(position).getRootView());
//        }
//
//        @Override
//        public Object instantiateItem(ViewGroup container, int position) {
//            container.addView(mLists.get(position).getRootView(), 0);
//            return mLists.get(position).getRootView();
//        }
//        @Override
//        public int getCount() {
//            // TODO Auto-generated method stub
//            return mLists.size();
//        }
//
//        @Override
//        public boolean isViewFromObject(View arg0, Object arg1) {
//            // TODO Auto-generated method stub
//            return arg0 == arg1;
//        }
    }

    public void finishActivity() {
        this.finish();
    }
}
